package cn.jeefast.rest.controller;


import cn.jeefast.common.entity.MbMessage;
import cn.jeefast.common.utils.R;
import cn.jeefast.rest.service.MbMessageService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import sun.nio.cs.ext.MacCentralEurope;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author theodo
 * @since 2020-01-02
 */
@RestController
@RequestMapping("/api")
public class MbMessageController {

    @Autowired
    private MbMessageService mbMessageService;

    @RequestMapping("/message/unRead")
    public R unRead(Long userId){
        EntityWrapper ew = new EntityWrapper();
        ew.eq("to_doc",userId);
        ew.eq("is_read",0);
        List list = mbMessageService.selectList(ew);
//        return R.ok().put("isRead",list.size()==0? false:true);
        Map isRead = new HashMap<>();
        isRead.put("isRead", list.size() == 0 ? false : true);
        return R.ok().put("data",isRead);
    }

    /**
     * 全部已读
     * @return
     */
    @RequestMapping("/message/readAll")
    public R readAll(Long userId){
        MbMessage mbMessage = new MbMessage();
        mbMessage.setIsRead(1);
        EntityWrapper ew = new EntityWrapper();
        ew.eq("to_doc",userId);
        boolean update = mbMessageService.update(mbMessage, ew);
        return R.ok("").put("data",null);
    }

    /**
     * 消息列表
     * @param userId
     * @return
     */
    @RequestMapping("/message/list")
    public R messageList(Long userId,Integer page,Integer limit){
        return mbMessageService.list(userId,page,limit);
    }
}